package vo
{
	import mx.collections.ArrayCollection;
	import mx.collections.IList;
	
	import util.DateConverter;
	
	[Bindable]
	public class Baby
	{
		public function Baby(xml:XML = null) {
			if (xml != null) {
				id = xml.id;
				firstname = toName(xml.firstname);
				lastname = toName(xml.lastname);
				dob = DateConverter.parseDateString(xml.dob);
				password = xml.password;
				gender = xml.gender;
				metric = xml.metric; 
				//selectedColor = xml.selectedColor;
				data = new ArrayCollection();
				for each (var dataXml:XML in xml.data.datapoint) {
					var datapoint:BabyData = new BabyData(dataXml);
					datapoint.day = Math.ceil((datapoint.date.time - dob.time) / (1000 * 60 * 60 * 24));
					data.addItem(datapoint);
				}
				var l:Number = data.length;
				if (l < 15) {
					for (var i:Number = 0; i < 15 - l; i++) {
						var blankdata:BabyData = new BabyData();
						data.addItem(blankdata);
					}
				}
			}
		}
		
		private function toName(n:String):String {
			if (n.length == 0) return "";
			var firstLetter:String = n.charAt(0);
			if (n.length > 1) {
				return firstLetter.toUpperCase() + n.substr(1);
			} else {
				return firstLetter.toUpperCase();
			}
		}
		
		public var id:Number;
		public var firstname:String;
		public var lastname:String;
		public var password:String;
		public var dob:Date;
		public var gender:Number = 1; 
		public var data:IList;
		public var metric:Number;
		//public var selectedColor:uint;
		
	}
}